Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: panic when pressing Shift + TAB in a TextEdit #3878

Closed
wants to merge 6 commits into from

Conversation

rustbasic
Copy link
Contributor

@rustbasic rustbasic commented Jan 24, 2024

panic when pressing Shift + TAB in a TextEdit #3846

rev() is panic with UTF8

Not byte_index
It's char_index ( for UTF8 )

panic when pressing Shift + TAB in a TextEdit emilk#3846
rev() is panic with UTF8

Not byte_index
It's char_index ( for UTF8 )
CCursor::new(line_start_index)
}

pub fn create_char_line_indexes(text: &str) -> Vec<usize> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you use CCursor instead of usize, the code becomes self-documenting

Suggested change
pub fn create_char_line_indexes(text: &str) -> Vec<usize> {
pub fn create_char_line_indexes(text: &str) -> Vec<CCursor> {

Copy link
Contributor Author

@rustbasic rustbasic Jan 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If CCursor is used instead of usize,
it seems handling functions might become difficult,
so I'm not sure if it's a good approach.
I will respect your final opinion.

By the way, the create_char_line_indexes() function is also used
in the next Pull Request related to TAB.

let position = text
.chars()
.rev()
.skip(chars_count - current_index.index)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was wrong with the old code?

Maybe it just needs to handle the char_count < current_index.index case?

Copy link
Contributor Author

@rustbasic rustbasic Jan 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since UTF-8 characters use 1 to 4 bytes,
using rev() can cause a panic and terminate the program.
You can test it by copying various UTF-8 characters
and using them in multiline().

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@rustbasic rustbasic Jan 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mix the UTF8 characters with English, fill the screen ( with them using multiline(), egui_demo_app, web demo ), and pressing Shift + TAB here and there and here and there.

Please let me know if you can't find the panic occurrence.
Let me explain it to you again.

Copy link
Contributor Author

@rustbasic rustbasic Jan 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's sample TEXT.
pressing Shift + TAB here and there and here and there.

    ABC äö 日本語 ABC äö 日本語                                                         
        ABC äö 日本語 ABC äö 日本語
               ABC äö 日本語 ABC äö 日本語
    ABC äö 日本語 ABC äö 日本語                                                         
        ABC äö 日本語 ABC äö 日本語
               ABC äö 日本語 ABC äö 日本語
    ABC äö 日本語 ABC äö 日本語                                                         
        ABC äö 日本語 ABC äö 日本語
               ABC äö 日本語 ABC äö 日本語

Copy link
Contributor Author

@rustbasic rustbasic Jan 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two more instances of rev() in text_cursor_state.rs,
fortunately, those two do not cause panics
but instead incorrectly select a block where UTF8 characters exist.
I will register this as a separate issue later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@emilk
Have you noticed the panic occurring?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have time for this right now. I believe you that here is a panic in egui, but I don't think the panic is in the call to .rev(), and so I think a much simpler solution to your problem can be found.

Run in debug mode and RUST_BACKTRACE=1 and paste the stack-trace to the actual panic.

rustbasic added a commit to rustbasic/egui that referenced this pull request Feb 6, 2024
@rustbasic rustbasic closed this Feb 24, 2024
emilk pushed a commit that referenced this pull request Mar 12, 2024
…3984)

* Closes #3846 
* Closes #3878

Dear emilk.

Leaving aside other function,
I think this is all you need to fix to patch the panic that occurs when
Shift + TAB.

Thank you, emilk.
@rustbasic rustbasic deleted the patch2 branch April 22, 2024 10:09
hacknus pushed a commit to hacknus/egui that referenced this pull request Oct 30, 2024
…milk#3984)

* Closes emilk#3846 
* Closes emilk#3878

Dear emilk.

Leaving aside other function,
I think this is all you need to fix to patch the panic that occurs when
Shift + TAB.

Thank you, emilk.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants